package com.jp.game.ui.home;

import static com.jp.game.common.Constant.NET_URL;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jp.game.R;
import com.jp.game.utils.SPUtil;

import java.util.List;

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.ViewHolder> {

    private List<Game> gameList;

    private Activity activity;

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ballLay;
        TextView oneTeamTv;
        TextView twoTeamTv;
        TextView ballTv;
        TextView timeTv;
        TextView shootTv;

        public ViewHolder(@NonNull View view) {
            super(view);
            ballLay = view.findViewById(R.id.ballLay);
            oneTeamTv = view.findViewById(R.id.oneTeamTv);
            twoTeamTv = view.findViewById(R.id.twoTeamTv);
            ballTv = view.findViewById(R.id.ballTv);
            timeTv = view.findViewById(R.id.timeTv);
            shootTv = view.findViewById(R.id.shootTv);
        }
    }

    public GameAdapter(Activity activity, List<Game> gameList) {
        this.activity = activity;
        this.gameList = gameList;
    }

    public void setNewList(List<Game> list) {
        this.gameList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Game game = gameList.get(position);
        holder.ballLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = game.getJumpUrl();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(SPUtil.getString("netUrl", NET_URL) + "/#/IP/EV" + url + "C1");
                intent.setData(content_url);
                activity.startActivity(intent);
            }
        });
        holder.oneTeamTv.setText(game.getOneTeam());
        holder.twoTeamTv.setText(game.getTwoTeam());
        holder.ballTv.setText(game.getBall());
        holder.timeTv.setText(game.getTime());
        holder.shootTv.setText(game.getShoot());
    }

    @Override
    public int getItemCount(){
        return gameList.size();
    }

}
